package cn.fengsweb.tokyotyrant
{
	import cn.fengsweb.tokyotyrant.define.TokyoTyrantQueryFunctionName;
	
	import flash.utils.ByteArray;
	
	internal class PutOneNoKeyHandler extends AbstractHandler
	{
		private var _putOneNoKey:PutOneNoKey;
		private var _uid:String;
		
		public function PutOneNoKeyHandler(host:String, port:uint)
		{
			super(host, port);
		}
		
		public function connect(putOneNoKey:PutOneNoKey, ttr:TokyoTyrantResponder) : void
		{
			_ttc			= new TokyoTyrantConnection(_host, _port, false);
			_ttr			= ttr;
			_putOneNoKey	= putOneNoKey;
			
			var query:TokyoTyrantQuery	= new TokyoTyrantQuery(TokyoTyrantQueryFunctionName.GENUID);
			defaultHandler(query.sendBytes, genuidHandler);
		}
		
		private function genuidHandler(recvBytes:ByteArray):void
		{
			var rnum:uint	= recvBytes.readUnsignedInt();
			var rsiz:uint	= recvBytes.readUnsignedInt();
			_uid			= recvBytes.readUTFBytes(rsiz);
			
			_putOneNoKey.setUid(_uid);
			defaultHandler(_putOneNoKey.sendBytes, successHandler);
		}
		
		private function successHandler(recvBytes:ByteArray):void
		{
			_ttr.result(_uid);
		}
	}
}